package com.wzdigit.wms.wms.service.core.outbound;
/**
 * 拣货任务表头 服务类
 *
 * @Author:yangwenbiao
 * @Date 2021/2/26 4:06 PM
 * @version 1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.wms.basic.domain.dtos.outbound.PicktaskDto;
import com.wzdigit.wms.basic.domain.transfer.PicktaskHeader;
import com.wzdigit.wms.basic.mapper.core.transfer.PicktaskHeaderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class PicktaskHeaderService {

    @Autowired
    PicktaskHeaderMapper picktaskHeaderMapper;

    /**
     * 获取拣货任务的详情
     * @param filter
     * @param loginName
     * @param allowStatusList
     */
    public List<PicktaskDto> getMainAndDtlInfo(String filter, String loginName, List<Integer> allowStatusList,Integer picktaskHeaderId) {

        List<PicktaskDto> picktaskDtoList = picktaskHeaderMapper.getMainAndDtlInfo(filter,loginName, allowStatusList,picktaskHeaderId);
        return picktaskDtoList;
    }

    /**
     * 查询任务详情的单头
     * @param loginName
     * @param picktaskHeaderId
     * @param picktastStatusList
     * @return
     */
    public List<PicktaskHeader> getHeaderInfo(String loginName, Integer picktaskHeaderId, List<Integer> picktastStatusList) {

        List<PicktaskHeader> picktaskHeader = picktaskHeaderMapper.getHeaderInfoOnDtl(loginName, picktaskHeaderId, picktastStatusList);

        return picktaskHeader;
    }

    /**
     * 根据主键查询实体
     * @param picktaskHeaderId
     * @return
     */
    public PicktaskHeader getByPk(Integer picktaskHeaderId) {
        PicktaskHeader picktaskHeader = picktaskHeaderMapper.selectById(picktaskHeaderId);
        return picktaskHeader;
    }


    /**
     * 根据主键更新单头状态
     * @param picktaskHeaderId
     * @param code
     * @param loginName
     * @param nowDate
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer updateStatusByPK(Integer picktaskHeaderId, int code, String loginName, Date nowDate) {

        PicktaskHeader picktaskHeader = new PicktaskHeader();
        picktaskHeader.setPicktaskHeaderId(picktaskHeaderId).setStatus(code);

        picktaskHeader.setModifyTime(nowDate);
        picktaskHeader.setModifyUser(loginName);

        int row = picktaskHeaderMapper.updateById(picktaskHeader);

        return row;
    }


    /**
     *
     * @param picktaskHeaderId
     * @param picktastStatusList
     * @return
     */
    public PicktaskHeader selectByHeadIdAndStatus(Integer picktaskHeaderId, List<Integer> picktastStatusList) {

        //创建类型的条件构造器
        QueryWrapper<PicktaskHeader> wrapper = new QueryWrapper<>();
        wrapper.eq("PICKTASK_HEADER_ID", picktaskHeaderId);
        wrapper.in("STATUS",picktastStatusList);

        PicktaskHeader picktaskHeader = picktaskHeaderMapper.selectOne(wrapper);

        return picktaskHeader;
    }

    /**
     * 添加一条拣货单头记录
     * @param picktaskHeader
     * @return
     */
    public Integer addOne(PicktaskHeader picktaskHeader) {

        int row = picktaskHeaderMapper.insert(picktaskHeader);
        return row;
    }

    public List<PicktaskHeader> selectByHeadIdList(List<Integer> headerIdList) {
        //创建类型的条件构造器
        QueryWrapper<PicktaskHeader> wrapper = new QueryWrapper<>();
        wrapper.in("PICKTASK_HEADER_ID", headerIdList);

        List<PicktaskHeader> picktaskHeaderList = picktaskHeaderMapper.selectList(wrapper);

        return picktaskHeaderList;
    }

    public PicktaskHeader selectByPickTaskNo(String picktaskNo) {
        //创建类型的条件构造器
        QueryWrapper<PicktaskHeader> wrapper = new QueryWrapper<>();
        wrapper.eq("PICKTASK_NO", picktaskNo);

        PicktaskHeader picktaskHeader = picktaskHeaderMapper.selectOne(wrapper);
        return picktaskHeader;
    }
}
